package com.java.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Calendar 日期类的常用转换
 */
public class CalendarUtil {

    static final String[] weeks = new String[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", };

    /**
     * Calendar 转换到 年月日
     * @param calendar
     * @return
     */
    public String calendarToExactDateStr(Calendar calendar){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }

    /**
     * Calendar 转换到时分秒
     * @param calendar
     * @return
     */
    public String calendarToHHmmss(Calendar calendar){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(calendar.getTime());
    }

    /**
     * Calendar 获取年
     * @param calendar
     * @return
     */
    public int getYear(Calendar calendar){
        return calendar.get(Calendar.YEAR);
    }

    /**
     * Calendar 获取月
     * @param calendar
     * @return
     */
    public int getMonth(Calendar calendar){
        // 比当前月份少1
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * Calendar 获取号
     * @param calendar
     */
    public int getDate(Calendar calendar){
        // date 表示日期，day 表示天数，所以 date 与 day_of_month 相同
        return calendar.get(Calendar.DATE);
    }

    /**
     * Calendar 获取号
     * @param calendar
     */
    public int getDayOfMonth(Calendar calendar){
        // date 表示日期，day 表示天数，所以 date 与 day_of_month 相同
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * Calendar 获取一周的第一天为星期
     */
    public int getWeek(){
        return Calendar.getInstance().getFirstDayOfWeek();
    }

    /**
     * Calendar 获取 T 的星期
     * @param calendar
     * @return
     */
    public String getDayOfWeek(Calendar calendar){
        return weeks[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    /**
     * Calendar 今年过了的天数
     */
    public int getDaysPassedThisYear(Calendar calendar){
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    /**
     * Calendar 获取小时（12小时制）
     * @param calendar
     * @return
     */
    public int getHour(Calendar calendar){
        return calendar.get(Calendar.HOUR);
    }

    /**
     * Calendar 获取小时（24小时制）
     * @param calendar
     * @return
     */
    public int getHourOfDay(Calendar calendar){
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * Calendar 获取分钟
     * @param calendar
     * @return
     */
    public int getMinute(Calendar calendar){
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * Calendar 获取秒数
     * @param calendar
     * @return
     */
    public int getSecond(Calendar calendar){
        return calendar.get(Calendar.SECOND);
    }

    /**
     * Calendar 获取毫秒数
     * @param calendar
     * @return
     */
    public int getMillisecond(Calendar calendar){
        return calendar.get(Calendar.MILLISECOND);
    }

    /**
     * Calendar 获取本月最后一天
     * @param calendar
     * @return
     */
    public int getMaxDate(Calendar calendar){
        return calendar.getActualMaximum(Calendar.DATE);
    }

    /**
     * Calendar 获取某个月最后一天
     * @param calendar
     * @return
     */
    public int getMaxDate(Calendar calendar, int month){
        calendar.set(calendar.get(Calendar.YEAR), month, 1);
        calendar.add(Calendar.DATE, -1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * Calendar 获取输入“年”“月”的最后一天
     * @param year
     * @param month
     * @return
     */
    public int getMaxDate(int year, int month){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);    //默认1月为0月
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取 n 年之后的时间
     * @param calendar 时间
     * @param years 表示 n 年之后
     * @return
     */
    public String getTimeAfterNYears(Calendar calendar,int years){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.add(Calendar.YEAR, years);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取 n 年之后的时间（中文：年月日）
     * @param calendar 时间
     * @param years 表示 n 年之后
     * @return
     */
    public String getTimeAfterNYearsInCh(Calendar calendar,int years){
        calendar.add(Calendar.YEAR, years);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        return year+"年"+month+"月"+day+"日";
    }

    /**
     * 输入日期是当前年的第 x 个星期
     * @param year
     * @param month
     * @param day
     * @return
     */
    public int getTheWeekOfYear(int year,int month,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH,month-1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 输入日期是当前月的第 x 个星期
     * @param year
     * @param month
     * @param day
     * @return
     */
    public int getTheWeekOfMonth(int year,int month,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH,month-1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar.get(Calendar.WEEK_OF_MONTH);
    }



}
